/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.ipccenter.entities;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Collection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;

/**
 *
 * @author sm-ni_000
 */
@Entity
@NamedQueries({@NamedQuery(name = "Article.findAuthor", query = "SELECT h FROM Article q, Human h WHERE q.title = :title"),
               @NamedQuery(name = "Article.findAllByAuthor", query = "SELECT q FROM Article q WHERE q.author = :author"),
               @NamedQuery(name = "Article.findByDate", query = "SELECT q FROM Article q WHERE q.creationTime >= :beginDate AND q.creationTime <= :endDate")})
public class Article implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    //@JoinColumn(referencedColumnName = "ID")
    @ManyToOne(fetch = FetchType.LAZY)
    private Human author;
   
    //@JoinColumn(referencedColumnName = "ID")
    @OneToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "ARTICLE_REF")
    private Collection<Article> references;
    
    private String title;
    private Timestamp creationTime;
    private String body;

    public Human getAuthor() {
        return author;
    }

    public void setAuthor(Human author) {
        this.author = author;
    }

    public Collection<Article> getReferences() {
        return references;
    }

    public void setReferences(Collection<Article> references) {
        this.references = references;
    }

    public Timestamp getCreationTime() {
        return creationTime;
    }

    public void setCreationTime(Timestamp creationTime) {
        this.creationTime = creationTime;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
    
    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }
    
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Article)) {
            return false;
        }
        Article other = (Article) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entities.Article[ id=" + id + " ]";
    }
    
}
